<script setup>
import { onMounted, ref } from 'vue';

const info = ref({})
const nfts = ref([])
const isNoTicket = ref(false)
const authParams = iconlakeAPI.auth.getSearchParams()

const vLazyload = {
  mounted(el, binding) {
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          if (binding.value) {
            if (el.tagName === 'IMG') {
              el.src = binding.value;
            } else {
              el.style.backgroundImage = `url(${binding.value})`;
            }
            observer.unobserve(el);
          }
        }
      });
    }, { threshold: 0.1 });
    
    observer.observe(el);
    
    el._observer = observer;
  },
  unmounted(el) {
    if (el._observer) {
      el._observer.unobserve(el);
      el._observer.disconnect();
    }
  }
};

async function getClassInfo() {
  info.value = await iconlakeAPI.class.getInfo(iconlakeAPI.class.id)
}

async function getNfts() {
  const res = await iconlakeAPI.class.getNfts(iconlakeAPI.class.id)
  if (res.nfts) {
    nfts.value = res.nfts
  } else if (res.error === 'noTicket') {
    isNoTicket.value = true
  }
}

function scrollToNfts() {
  document.querySelector('.list').scrollIntoView({ behavior: 'smooth' })
}

function loadShare() {
  iconlakeAPI.share.load(document.querySelector('.share'), {
    classId: info.value.id
  })
}

onMounted(() => {
  getClassInfo().then(() => {
    iconlakeAPI.loading.isShow = false
    loadShare()
  })
  getNfts()
})
</script>

<template>
  <div v-if="info.uri || info.name" class="class" @click="scrollToNfts">
    <div class="cover" :style="`background-image: url(${info.uri})`"></div>
    <div class="title">{{ info.name }}</div>
    <div class="desc">{{ info.description }}</div>
    <div class="author">Created by <a :href="`/exhibition/creator/${info.data?.author}`">{{ info.data?.author }}</a> <span class="share"></span></div>
  </div>
  <div class="list" v-if="nfts.length">
    <a v-for="nft in nfts" class="item" :href="`/exhibition/${info.id}/${nft.id}${authParams ? '?' + authParams : ''}`">
      <div class="cover" v-lazyload="`${nft.uri}?imageMogr2/thumbnail/600x600>/ignore-error/1`"></div>
      <p class="title">{{ nft.data?.name }}</p>
    </a>
  </div>
  <div class="no-ticket" v-if="isNoTicket">
    <h1>云门半掩，金钥未至，暂隔蓬莱烟霞。</h1>
  </div>
</template>

<style lang="scss" scoped>
.class {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  color: #fff;
  font-family: sans-serif;
  line-height: 1.8;
  padding: 0 80px;

  .cover {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    background-size: cover;
  }

  .title {
    font-size: 50px;
    font-weight: bold;
    position: relative;
  }

  .desc {
    font-size: 18px;
    margin-bottom: 40px;
    position: relative;
  }

  .author {
    font-size: 12px;
    position: absolute;
    left: 0;
    bottom: 20px;
    right: 0;
    text-align: center;

    a {
      color: #fff;
      text-decoration: underline;
      text-underline-offset: 3px;
    }
  }

  .share {
    display: inline-block;
    line-height: 1;
    vertical-align: middle;
    cursor: pointer;
  }
}

.list {
  display: flex;
  flex-wrap: wrap;
  margin-top: 30px;
  font-family: sans-serif;
}

.item {
  width: 12.5vw;
  text-align: center;
  color: #000;
  margin-bottom: 30px;
  text-decoration: none;
  transition: all 0.3s;

  &:hover {
    transform: scale(1.1);
  }

  .cover {
    width: 8vw;
    height: 8vw;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    margin: 0 auto;
  }

  .title {
    margin: 0;
    padding: 10px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 12px;
  }
}

.no-ticket {
  text-align: center;
  padding: 100px 0;
}
</style>

<style lang="scss">
.st-inline-share-buttons {
  .st-btn {
    background-color: transparent !important;
  }
}
</style>